package com.mastercard.assets.web.struts.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.jpa.TblAlerta;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AlertasEditarForm;
import com.mastercard.assets.web.struts.forms.AlertasForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class AlertasIndexIn
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		int role = user.getActualRoleId( ) ;
		
		if( role != 2 )
			return mapping.findForward( "RoleInvalid" ) ;
		
		// toma el form
		AlertasForm f = ( AlertasForm ) form ;
		
		// en caso de null
		if( f == null )
			return mapping.findForward( "SourceInvalid" ) ;
		
		// toma los datos
		int cveAlerta = f.getCveAlerta( ) , operation = f.getOperation( ) ,
                        idioma = user.getIdioma( ) ;
		
		TblAlerta alerta = null ;
		
		// revisa operacion
		switch( operation )
		{
			// en caso de agregar forma
			case 1 :
                                if( idioma == 0 ) {
                                    return mapping.findForward( "AddAlerta" ) ;
                                } else 
                                    return mapping.findForward( "AddAlertaEng" ) ;
				
			// en caso de eliminar alerta
			case 2 :
				
				// levanta la forma
				alerta = MasterCardDAO.findAlertById( cveAlerta ) ;
				
				AlertasForm alertasForm = new AlertasForm( ) ;
				
				alertasForm.setCveAlerta( cveAlerta ) ;
				
				request.setAttribute( "AlertasForm" , alertasForm ) ;
				request.setAttribute( "Alerta" , alerta ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "RemoveAlerta" ) ;
                                } else 
                                    return mapping.findForward( "RemoveAlertaEng" ) ;
				
				
//			// en caso de editar forma
			case 3 :
				
				// levanta la forma
				alerta = MasterCardDAO.findAlertById( cveAlerta ) ;
				
				// lo coloca en el form adecuado
				AlertasEditarForm alertaEditar = new AlertasEditarForm( ) ;
				
				alertaEditar.setCveAlerta( alerta.getCveAlerta( ) ) ;
				alertaEditar.setDescripcion( alerta.getDescripcion( ) ) ;
				alertaEditar.setNombre( alerta.getNombre( ) ) ;
				alertaEditar.setTexto( alerta.getTexto( ) ) ;
				
				request.setAttribute( "AlertasEditarForm" , alertaEditar ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
                                if( idioma == 0 ) {
                                    return mapping.findForward( "EditAlerta" ) ;
                                } else 
                                    return mapping.findForward( "EditAlertaEng" ) ;
				
				
			// cualquier otro caso
			default :
		}
		
                MasterCardDAO.closeEntityManager( ) ;
		return mapping.findForward( "success" ) ;
	}
	
}
